/* 用户注册 */
const router = require("koa-router")();
import { ReadFile, WriteFile } from "../utils/ReadWriteFile";
import { createId } from "../utils/createId";
/* 查询所有的普通用户*/
router.post("/api/Retrieve", async (ctx) => {
  const { username, password, type } = ctx.request.body;
  /* 只有包含用户才添加 */
  let result = await ReadFile(type);
  const res = result.find((item) => item.username == username);
  console.log(res);
  if (res) {
    if (password && username) {
      res.password = password
      let res1 = result.filter((item) => item.username != username);
      res1.push(res);
      WriteFile({
        type,
        result:res1,
      });
      // jsonfile.writeFileSync(process.cwd()+`/data/common.json`,result)
      ctx.body = {
        code: 200,
        msg: "修改成功",
      };
    } else {
      ctx.body = {
        code: 400,
        msg: "参数不合法",
      };
    }
  } else {
    ctx.body = {
      code: 400,
      msg: "用户名不存在,请注册",
    };
  }
});
module.exports = router;
